package com.pachong.android.frameworkbase.utils.io;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

/**
 * 工具类，基础的SP get set操作， 实现基础类型的增删改查、另外加一个序列化
 * 不要求做到每一种方法都封装到，只封装常用的方法，不常用不封装
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/3/31 17:48
 * @Version: 1.0.0
 */
public class SharePreferenceUtil {

    public static void saveInt(Context context, String spFilename, String key, int intValue) {
        SharedPreferences prefes = context.getSharedPreferences(spFilename, 0);
        SharedPreferences.Editor editor = prefes.edit();
        editor.putInt(key, intValue);
        editor.commit();
    }

    public static int getInt(Context context, String spFilename, String key) {
        return context.getSharedPreferences(spFilename, 0).getInt(key, -1);
    }


    public static void saveFloat(Context context, String spFilename, String key, float fValue) {
        SharedPreferences prefes = context.getSharedPreferences(spFilename, 0);
        SharedPreferences.Editor editor = prefes.edit();
        editor.putFloat(key, fValue);
        editor.commit();
    }


    public static float getFloat(Context context, String spFilename, String key) {
        return context.getSharedPreferences(spFilename, 0).getFloat(key, 0.0f);
    }


    public static void saveString(Context context, String spFilename, String key, String strValue) {
        SharedPreferences prefes = context.getSharedPreferences(spFilename, 0);
        SharedPreferences.Editor editor = prefes.edit();
        editor.putString(key, strValue);
        editor.commit();
    }


    public static String getString(Context context, String spFilename, String key) {
        return context.getSharedPreferences(spFilename, 0).getString(key, "");
    }


    public static void saveBoolean(Context context, String spFilename, String key, boolean bValue) {
        SharedPreferences prefes = context.getSharedPreferences(spFilename, 0);
        SharedPreferences.Editor editor = prefes.edit();
        editor.putBoolean(key, bValue);
        editor.commit();
    }


    public static Boolean getBoolean(Context context, String spFilename, String key) {
        return getBoolean(context, spFilename, key, false);
    }


    public static Boolean getBoolean(Context context, String spFilename, String key, boolean defBoolean) {
        return context.getSharedPreferences(spFilename, 0).getBoolean(key, defBoolean);
    }


    public static void saveLong(Context context, String spFilename, String key, long lValue) {
        SharedPreferences prefes = context.getSharedPreferences(spFilename, 0);
        SharedPreferences.Editor editor = prefes.edit();
        editor.putLong(key, lValue);
        editor.commit();
    }


    public static Long getLong(Context context, String spFilename, String key) {
        return context.getSharedPreferences(spFilename, 0).getLong(key, 0L);
    }


    public static void saveObject(Context context, String spFilename, String key, Object objValue) {
        SharedPreferences sp = context.getSharedPreferences(spFilename, Context.MODE_PRIVATE);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(baos);
            out.writeObject(objValue);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, objectVal);
            editor.commit();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static <T> T getObject(Context context, String spFilename, String key, Class<T> clazz) {
        SharedPreferences sp = context.getSharedPreferences(spFilename, Context.MODE_PRIVATE);
        if (sp.contains(key)) {
            String objectVal = sp.getString(key, null);
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                T t = (T) ois.readObject();
                return t;
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 清除某个sp文件
     *
     * @param context
     * @param spFilename
     */
    public static void clear(Context context, String spFilename) {
        SharedPreferences prefes = context.getSharedPreferences(spFilename, 0);
        SharedPreferences.Editor editor = prefes.edit();
        editor.clear();
        editor.commit();
    }

}
